//
//  ProductTableViewCell.m
//  SnowCovered
//
/*
 
 Copyright (c) 2010, Subodh Shakya, Anand Subba Rao, All Rights Reserved
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 
 */
#import "ProductTableViewCell.h"
#import <QuartzCore/QuartzCore.h>

@interface ProductTableViewCell()
@property (nonatomic, retain) UIImageView* productImage;
@property (nonatomic, retain) UILabel* publisherName;
@property (nonatomic, retain) UILabel* productName;
@property (nonatomic, retain) UILabel* productPrice;
@property (nonatomic, retain) UILabel* publishedDate;
@end

@implementation ProductTableViewCell
@synthesize product;
@synthesize productImage;
@synthesize publisherName;
@synthesize productName;
@synthesize productPrice;
@synthesize publishedDate;
@synthesize delegate;
@synthesize featured;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code
        productImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        CALayer* layer = [self.productImage layer];
        [layer setCornerRadius:5.0f];
        [layer setBorderWidth:1.0f];
        layer.masksToBounds = YES;
        [productImage setContentMode:UIViewContentModeScaleAspectFit];
        
        productName = [[UILabel alloc] initWithFrame:CGRectZero];
        self.productName.font = [UIFont boldSystemFontOfSize:14];
        self.productName.highlightedTextColor = [UIColor whiteColor];
        
        publisherName = [[UILabel alloc] initWithFrame:CGRectZero];
        self.publisherName.font = [UIFont systemFontOfSize:12];
        self.publisherName.highlightedTextColor = [UIColor whiteColor];

        publishedDate = [[UILabel alloc] initWithFrame:CGRectZero];
        self.publishedDate.font = [UIFont systemFontOfSize:12];
        self.publishedDate.highlightedTextColor = [UIColor whiteColor];
        
        productPrice = [[UILabel alloc] initWithFrame:CGRectZero];
        self.productPrice.highlightedTextColor = [UIColor whiteColor];
        self.productPrice.font = [UIFont boldSystemFontOfSize:12];
        self.productPrice.textAlignment = UITextAlignmentRight;
        [productPrice setAdjustsFontSizeToFitWidth:YES];
        
        [self.contentView addSubview:productImage];
        [self.contentView addSubview:publisherName];
        [self.contentView addSubview:productName];
        [self.contentView addSubview:publishedDate];
        [self.contentView addSubview:productPrice];                                            
    }
    return self;
}

#define kImageHorizPadding 10.0
#define kImageVertPadding 10.0
#define kImageSize 40.0
#define kImageRightPadding 5.0
#define kInfoVertPadding 5.0
#define kPublisherNameHeight 13.0
#define kProductNameHeight 18.0
#define kProductPriceWidth 35.0
#define kProductPriceHeight 20.0
#define kPublishedDateHeight 15.0

#define kFProductNameMargin 10.0
#define kFProductNameTopMargin 5.0
#define kFImageHeight 110

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect rect = [self.contentView bounds];
    
    if (!featured) {
        self.productImage.frame = CGRectMake(rect.origin.x + kImageHorizPadding,
                                        rect.origin.y + kImageVertPadding, 
                                        kImageSize,
                                        kImageSize);
        self.publisherName.frame = CGRectMake(CGRectGetMaxX(productImage.frame) + kImageRightPadding,
                                         kInfoVertPadding,
                                         CGRectGetWidth(rect) - kProductPriceWidth - kImageSize - kImageRightPadding, 
                                         kPublisherNameHeight);
        
        self.productName.frame = CGRectMake(CGRectGetMaxX(productImage.frame) + kImageRightPadding,
                                       CGRectGetMaxY(publisherName.frame) + 1,
                                       CGRectGetWidth(rect) - kProductPriceWidth - kImageSize - kImageRightPadding,
                                       kProductNameHeight);
    } else {
        self.productName.frame = CGRectMake(kFProductNameMargin, kFProductNameTopMargin,
                                            CGRectGetWidth(rect) - kFProductNameMargin,
                                            kProductNameHeight * 2.0);
        CGRect tmp = CGRectInset(self.productName.frame, 20, 10);
        tmp.origin.y = CGRectGetMaxY(self.productName.frame) + 1;
        self.publisherName.frame = tmp;
        
        self.productImage.frame = CGRectMake(kFProductNameMargin, CGRectGetMaxY(self.publisherName.frame) + 1,
                                             CGRectGetWidth(rect) - kFProductNameMargin,
                                             kFImageHeight);
    }
}

- (void)setFeatured:(BOOL)inFeatured {
    if (inFeatured) {
        [[self.productImage layer] setBorderColor:[[UIColor clearColor] CGColor]];
        [self.productName setNumberOfLines:2];
    } else {
        [[self.productImage layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
        [self.productName setNumberOfLines:1];
    }
    featured = inFeatured;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)dealloc {
    self.product = nil;
    self.productImage = nil;
    self.productName = nil;
    self.productPrice = nil;
    self.publisherName = nil;
    self.publishedDate = nil;
    [super dealloc];
}

+ (CGFloat)requiredHeight {
    return (2* kImageVertPadding + kImageSize);
}

+ (CGFloat)requiredFeatureHeight {
    return [ProductTableViewCell requiredHeight] * 3.0;
}

- (void)longPress:(UIGestureRecognizer*)gestureRecognizer {
    [self.delegate showDetailsForRowAtIndexPath:[(UITableView*)[self superview] indexPathForCell:self]];
}
@end

//    self.publishedDate.frame = CGRectMake(CGRectGetMaxX(productImage.frame) + kImageRightPadding,
//                                          CGRectGetMaxY(self.productName.frame) + 1,
//                                          CGRectGetWidth(rect) - kProductPriceWidth - kImageSize - kImageRightPadding,
//                                          kPublishedDateHeight);
//        
//    self.productPrice.frame = CGRectMake(CGRectGetMaxX(rect) - kProductPriceWidth,
//                                    floor((CGRectGetHeight(rect) - kProductPriceHeight) / 2.0),
//                                    kProductPriceWidth,
//                                    kProductPriceHeight);
